﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Physio10Context;
using System.Collections;
using DatabaseManager;

namespace Physio_Clinic
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        Drug current_drug = null;
        public Qf8soUser currentUser = null;
        public bool isnonDrug = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["nonDrug"] = null;

            if (Session["currentUser"] != null)
            {
                currentUser = (Qf8soUser)Session["currentUser"];
            }

            if (Request.QueryString["nonDrug"] != null)
            {
                isnonDrug = false;
                Session["nonDrug"] = 1;

                Label2.Text = "การรักษา";

                GridView1.Columns[2].Visible = false;
                GridView1.Columns[3].HeaderText = "การรักษา";
                GridView1.Columns[3].ItemStyle.Width = 300;
                GridView1.Columns[6].Visible = false;
                
            }

            DbLinqDataSource.WhereParameters[0].DefaultValue = isnonDrug.ToString();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedRow != null)
            {
                string value1 = GridView1.SelectedRow.Cells[1].Text;

                current_drug = PersistentManager.Instance.GetDrug(Convert.ToInt32(value1));

                Session["currentDrug"] = current_drug;

                Button2.Enabled = true;
                Button3.Enabled = true;
                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Session["currentDrug"] != null)
            {
                PersistentManager.Instance.Delete((Drug)Session["currentDrug"], currentUser.Username);
                PersistentManager.Instance.Save();
            }

            Response.Redirect("./DrugStock.aspx");
        }

        protected void DbLinqDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["nonDrug"] != null)
            {
                Response.Redirect("./DrugDetail.aspx?nonDrug=1");
            }
            else
                Response.Redirect("./DrugDetail.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Session["currentDrug"] != null)
            {
                if (Session["nonDrug"] != null)
                {
                    Response.Redirect("./DrugDetail.aspx?nonDrug=1");
                }
                else
                    Response.Redirect("./DrugDetail.aspx");
            }
        }
    }
}